package middleware

import (
	"context"
	"github.com/cy77cc/hioshop/global"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 限流中间件
func LimitAccess() gin.HandlerFunc {
	return func(c *gin.Context) {
		res := global.REDIS.LPop(context.Background(), "limit_list")
		if res.Err() != nil {
			c.JSON(http.StatusBadRequest, "超出流量限制")
			c.Abort()
			return
		}
		c.Set("limitToken", res.String())
		c.Next()
	}
}
